<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

require APP_ROOT_PATH.'app/Lib/uc.php';

class uc_collectModule extends ShopBaseModule
{
	public function index()
	{
		
		$page = intval($_REQUEST['p']);
		if($page==0)
		$page = 1;
		$limit = (($page-1)*app_conf("PAGE_SIZE")).",".app_conf("PAGE_SIZE");
		$result = get_collect_list($limit,$GLOBALS['user_info']['id']);
		
		$GLOBALS['tmpl']->assign("list",$result['list']);
		$page = new Page($result['count'],app_conf("PAGE_SIZE"));   //初始化分页对象 		
		$p  =  $page->show();
		$GLOBALS['tmpl']->assign('pages',$p);
		
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['UC_COLLECT']);
		$GLOBALS['tmpl']->assign("inc_file","inc/uc/uc_collect_index.html");
	
				
		$GLOBALS['tmpl']->display("uc.html");
	}
	
	public function del()
	{
		$id = intval($_REQUEST['id']);
		$GLOBALS['db']->query("delete from ".DB_PREFIX."deal_collect where id = ".$id." and user_id = ".intval($GLOBALS['user_info']['id']));
		if($GLOBALS['db']->affected_rows())
		{
			showSuccess($GLOBALS['lang']['DELETE_SUCCESS']);
		}
		else
		{
			showErr($GLOBALS['lang']['INVALID_COLLECT']);
		}
	}
	
	public function store(){
		$page = intval($_REQUEST['p']);
		if($page==0)
		$page = 1;
		$limit = (($page-1)*app_conf("PAGE_SIZE")).",".app_conf("PAGE_SIZE");
		
		$user_id = intval($GLOBALS['user_info']['id']);
		$sql = "select d.name,d.id,c.sid,c.create_time from ".DB_PREFIX."user_fav_location as c " .
				"left join ".DB_PREFIX."supplier_location as d on d.id = c.sid where c.uid = ".$user_id." order by c.create_time desc limit ".$limit;
		$sql_count = "select count(*) from ".DB_PREFIX."user_fav_location where uid = ".$user_id;
		$result['list'] = $GLOBALS['db']->getAll($sql);
		$result['count'] = $GLOBALS['db']->getOne($sql_count);
		
		$GLOBALS['tmpl']->assign("list",$result['list']);
		$page = new Page($result['count'],app_conf("PAGE_SIZE"));   //初始化分页对象 		
		$p  =  $page->show();
		$GLOBALS['tmpl']->assign('pages',$p);
		
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['UC_STORE_FAV']);
		$GLOBALS['tmpl']->assign("inc_file","inc/uc/uc_collect_store_index.html");
	
				
		$GLOBALS['tmpl']->display("uc.html");
	}
	
	public function del_store(){
		$id = intval($_REQUEST['id']);
		$GLOBALS['db']->query("delete from ".DB_PREFIX."user_fav_location where sid = ".$id." and uid = ".intval($GLOBALS['user_info']['id']));
		if($GLOBALS['db']->affected_rows())
		{
			showSuccess($GLOBALS['lang']['DELETE_SUCCESS']);
		}
		else
		{
			showErr($GLOBALS['lang']['INVALID_COLLECT']);
		}
	}
	
	public function menu(){
	
		$page = intval($_REQUEST['p']);
		if($page==0)
			$page = 1;
		$limit = (($page-1)*app_conf("PAGE_SIZE")).",".app_conf("PAGE_SIZE");
		$keyword =  trim($_REQUEST['keyword']);
		$ext_where= "";
		if($keyword!=""){
			$ext_where = " AND sl.name like '%".htmlspecialchars(addslashes($keyword))."%'";
		}
		
		$list = $GLOBALS['db']->getAll("SELECT ufm.id,ufm.name,ufm.supplier_location_id as sid,ufm.cache_data,sl.name as location_name,sl.preview FROM ".DB_PREFIX."user_fav_menu ufm LEFT JOIN ".DB_PREFIX."supplier_location sl ON sl.id=ufm.supplier_location_id WHERE ufm.user_id=".intval($GLOBALS['user_info']['id'])." $ext_where order by ufm.id DESC LIMIT ".$limit);
		
		foreach($list as $k=>$v){
			$list[$k]['menu'] = unserialize($v['cache_data']);
		}
		
		$count = $GLOBALS['db']->getOne("SELECT COUNT(DISTINCT ufm.id) FROM ".DB_PREFIX."user_fav_menu ufm LEFT JOIN ".DB_PREFIX."supplier_location sl ON sl.id=ufm.supplier_location_id WHERE ufm.user_id=".intval($GLOBALS['user_info']['id']).$ext_where);
		
		$page = new Page($count,app_conf("PAGE_SIZE"));   //初始化分页对象 		
		$p  =  $page->show();
		$GLOBALS['tmpl']->assign('pages',$p);
		$GLOBALS['tmpl']->assign('list',$list);
		$GLOBALS['tmpl']->assign('count',$count);
		
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['UC_MENU_FAV']);
		$GLOBALS['tmpl']->assign("inc_file","inc/uc/uc_collect_menu_index.html");
		$GLOBALS['tmpl']->display("uc.html");
	}
	
	public function updatemenu(){
		$return = array("status"=>1,"message"=>"");
		$id = intval($_REQUEST['id']);
		$val = htmlspecialchars(addslashes(trim($_REQUEST['val'])));
		if($id==0 || $val==""){
			$return['status']=0;
			$return['message']="处理数据错误！";
			ajax_return($return);
		}
		
		if($GLOBALS['db']->autoExecute(DB_PREFIX."user_fav_menu",array("name"=>$val),"UPDATE","id=$id AND user_id=".intval($GLOBALS['user_info']['id']))){
			$return['status']=1;
			ajax_return($return);
		}
		else{
			$return['status']=0;
			$return['message']="处理数据错误！";
			ajax_return($return);
		}
		
	}
	
	public function del_menu(){
		$return = array("status"=>1,"message"=>"");
		$id = intval($_REQUEST['id']);
		if($id==0){
			$return['status']=0;
			$return['message']="处理数据错误！";
			ajax_return($return);
		}
		$GLOBALS['db']->query("delete from ".DB_PREFIX."user_fav_menu where id = ".$id." and user_id = ".intval($GLOBALS['user_info']['id']));
		if($GLOBALS['db']->affected_rows()){
			$return['status']=1;
			ajax_return($return);
		}
		else{
			$return['status']=0;
			$return['message']="处理数据错误！";
			ajax_return($return);
		}
	}
}
?>